<?php 
	include_once('Common.php');
	class Job extends DModel{
		protected $_name = 'job';
		function find_job(){
			$sql = "SELECT * FROM  (SELECT * FROM job ORDER BY job_id desc) a where a.status= 'valid' GROUP BY a.com_id ORDER BY a.job_id DESC";
			return  $this->getAdapter()->fetchAll($sql);
		}
		
		function find_new_job(){
			$sql = "SELECT * FROM job AS k  where Status = 'valid' And Deadline>curdate() ORDER BY Job_ID DESC";
			$logger = new Zend_Log();
			$writer = new Zend_Log_Writer_Stream('./log/development.txt');
			$logger->addWriter($writer);
			$data = array();
			try{
				$data = $this->getAdapter()->fetchAll($sql);
			}catch(Exception $e){
				$logger->log($sql, Zend_Log::DEBUG);
				$logger->log(dump($param_array), Zend_Log::DEBUG);
			}
				return $data ;
			
		}
		
		function  find_job_by_condition($option = array()){
			if(empty($option)){
				return $this -> find_all(array('order' => 'job_id DESC'));
			}else{
				$param_array = array();
				$sql = "SELECT * FROM job WHERE status = 'valid' ";
					
				if(isset($option['category'])){
					$param_array = array_merge($param_array,$option['category']);
					$str = "";
					foreach($option['category'] as $value){
						$str .= "?" . ",";
					}
					$str = chop($str, ",");
					$str = "(" . $str . ")";
					$sql .= "AND JobCat IN " . $str;
				}
				
				if(isset($option['location'])){
					$sql .= "AND (";
					for($i=0;$i< count($option['location']);$i++){
						if($i < count($option['location']) - 1){
							$sql .= "WorkPlace LIKE ? OR ";
							
						}else{
							$sql .= " WorkPlace LIKE ? )";
						}
						array_push($param_array,'%' . $option['location'][$i] . '%');
					}
				}
				
				if(isset($option['workstyle'])){
					$param_array = array_merge($param_array,$option['workstyle']);
					$str = "";
					foreach($option['workstyle'] as $value){
						$str .= "?" . ",";
					}
					$str = chop($str, ",");
					$str = "(" . $str . ")";
					$sql .= "AND JobType IN " . $str;
				}
				if(isset($option['keyword'])){
						$sql.= " AND (JobTitle LIKE ? OR ComName LIKE ?  
							OR JobType LIKE ?  OR JobDesc LIKE ? 
							OR ComAddress LIKE ? OR ComProfile LIKE ?
							OR JobLevel LIKE ? OR Benefits LIKE ?
							OR JobReq LIKE ? OR WorkPlace LIKE ? OR SalaryRange LIKE ? )";  
						for($i=0;$i<11;$i++){
							array_push($param_array,'%' . $option['keyword'] . '%');
						}
				}
				$logger = new Zend_Log();
				$writer = new Zend_Log_Writer_Stream('./log/development.txt');
				$logger->addWriter($writer);
				$data = array();
			  try{
						$data = $this->getAdapter()->fetchAll($sql,$param_array);

				}catch(Exception $e){
					$logger->log($sql, Zend_Log::DEBUG);
					$logger->log(dump($param_array), Zend_Log::DEBUG);
				}
				return $data ;
			
			}
		}
	}
?>